#!/bin/bash
# $Id: make_debp,v 1.0.1 2009/01/13 14:34:51 ricardo Exp $
# Written by Ricardo Carlini Sperandio <rcarlini@gmail.com>.  
# Distributed under the GNU GPL

pacote=$1

cp -a "$pacote" "$pacote-empacotando"
chown -R root.root $pacote

if [ $# -lt 1 ]; then
  echo "Nao foi informado o diretorio base do pacote."
  exit 1
fi

if [ ! -d $pacote/DEBIAN ]; then
  echo "Nao foi possivel encontrar o diretorio $pacote/DEBIAN"
  exit 1
fi

if [ ! -f $pacote/DEBIAN/control ]; then
  echo "Nao foi possivel encontrar o arquivo de controle (control)"
  exit 1
fi
###################################
# Pega os dados atuais no control #
###################################

pacoteName=$(cat ${pacote}/DEBIAN/control | grep ^Package: | sed 's/^Package: \(.*\)/\1/' )
# pega a versao
versao=$(cat ${pacote}/DEBIAN/control | grep ^Version | sed 's/^Version: \(.*\)/\1/' )
# pega a arquitetura
arch=$(cat ${pacote}/DEBIAN/control | grep ^Architecture | sed 's/^Architecture: \(.*\)/\1/' )
# gera nome do pacote
nomeRelease=${pacote}_${versao}_${arch}.deb


#########################
# Prepara empacotamento #
#########################
# remove os arquivos temporarios (na verdade move para um diretorio trash)
listaLixo=$(find $pacote -regextype posix-egrep  -iregex '.*(~|\.(tmp|old|bak))' -print0 | xargs -0 )

    rm -f $listaLixo

listaLixo2=$(find $pacote -regextype posix-egrep  -iregex '.*(.(svn|directory))' -print0 | xargs -0 )

    rm -Rf $listaLixo2

# gera arquivo md5sum
if [ -f $pacote/DEBIAN/md5sums ]; then
   rm -f $pacote/DEBIAN/md5sums
fi
find $pacote ! -type d -print0 | xargs -0 md5sum > $pacote/DEBIAN/md5sums

# gera instaled size
size=$(du -s $pacote | cut -f 1)
sed -i "s/^\(Installed-Size:\) .*/\1 $size/" ${pacote}/DEBIAN/control 


######################################
# Atualiza o control automaticamente #
######################################

# atualiza o release para as proximas contrucoes
# linha onde esta o campo Version
linha=$(grep -n ^Version  ${pacote}/DEBIAN/control | cut -d ":" -f 1)
# gera o update do debian release

#TODO: pedir confirmacao.
complemento=$(grep  ^Version $pacote/DEBIAN/control | sed "s/^Version: \(.*\)-\([0-9]\+\)\(.*\)/\3/")
release=$(grep  ^Version $pacote/DEBIAN/control | sed "s/^Version: \(.*\)-\([0-9]\)\(.*\)/\1/")

releaseDeb=$(grep  ^Version $pacote/DEBIAN/control | sed "s/^Version: \(.*\)-\([0-9]\+\)\(.*\)/\2/")

: $((releaseDeb++))

# echo -n "Deseja atualizar o Debrelease para $releaseDeb? (s/N): "
# read opcao
if [ "$opcao"  = "s" ]; then
   sed -i "$linha s/^Version: .\+/Version: $release-$releaseDeb$complemento/" $pacote/DEBIAN/control
   versao=$release-$releaseDeb$complemento   
   nomeRelease=${pacote}_${versao}_${arch}.deb
fi
########################
# constroe o pacote... # 
########################
dpkg -b $pacoteName $nomeRelease

######################################
# Atualiza o control automaticamente #
######################################

# atualiza o release para as proximas contrucoes
# linha onde esta o campo Version
linha=$(grep -n ^Version  ${pacote}/DEBIAN/control | cut -d ":" -f 1)
# gera o update do debian release
#TODO: pedir confirmacao.
complemento=$(grep  ^Version $pacote/DEBIAN/control | sed "s/^Version: \(.*\)-\([0-9]\+\)\(.*\)/\3/")
release=$(grep  ^Version $pacote/DEBIAN/control | sed "s/^Version: \(.*\)-\([0-9]\)\(.*\)/\1/")

releaseDeb=$(grep  ^Version $pacote/DEBIAN/control | sed "s/^Version: \(.*\)-\([0-9]\+\)\(.*\)/\2/")

: $((releaseDeb++))

# echo -n "Deseja atualizar o Debrelease para $releaseDeb?(valido apenas para o proximo pacote) (s/N): "
# read opcao
if [ "$opcao"  = "s" ]; then
   sed -i "$linha s/^Version: .\+/Version: $release-$releaseDeb$complemento/" $pacote/DEBIAN/control
fi

# remove o md5sums...
if [ -f $pacote/DEBIAN/md5sums ]; then
   rm -f $pacote/DEBIAN/md5sums
fi

rm -Rf $pacote
mv -f "$pacote-empacotando" "$pacote"